package com.sskj.gcc.ui.fragment;


import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.bean.SellInfo;
import com.sskj.gcc.ui.activity.SellRecordsActivity;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.MoneyValueFilter;
import com.sskj.lib.base.BaseFragment;

import java.time.chrono.IsoEra;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author Hey
 * Create at  2019/07/29 15:08:08
 */
public class OneSellFragment extends BaseFragment<OneSellPresenter> {


    @BindView(R.id.sell_price_tv)
    TextView sellPriceTv;
    @BindView(R.id.seller_count_tv)
    TextView sellerCountTv;
    @BindView(R.id.useful_num_tv)
    TextView usefulNumTv;
    @BindView(R.id.edt_num)
    EditText edtNum;
    @BindView(R.id.arrive_num_tv)
    TextView arriveNumTv;
    @BindView(R.id.edt_name)
    EditText edtName;
    @BindView(R.id.edt_bank)
    EditText edtBank;
    @BindView(R.id.edt_bank_num)
    EditText edtBankNum;
    @BindView(R.id.edt_bank_branch)
    EditText edtBankBranch;
    @BindView(R.id.edt_ps)
    EditText edtPs;
    @BindView(R.id.submit_btn)
    Button submitBtn;

    private double price;
    private String msg;
    private boolean isEnable;

    @Override
    public int getLayoutId() {
        return R.layout.app_fragment_one_sell;
    }

    @Override
    public OneSellPresenter getPresenter() {
        return new OneSellPresenter();
    }

    @Override
    public void initView() {
        edtNum.setFilters(new InputFilter[]{new MoneyValueFilter(4)});
        edtNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                computeTotal();
            }
        });
    }


    @Override
    public void initData() {
        mPresenter.getInfo();
        ClickUtil.click(submitBtn, () -> {
            if (!isEnable) {
                ToastUtil.showShort(msg == null ? App.INSTANCE.getString(R.string.one_buy_maintenance) : msg);
                return;
            }

            if (TextUtils.isEmpty(edtNum.getText())) {
                ToastUtil.showShort(edtNum.getHint());
                return;
            }
            if (TextUtils.isEmpty(edtBank.getText())) {
                ToastUtil.showShort(edtBank.getHint());
                return;
            }
            if (TextUtils.isEmpty(edtName.getText())) {
                ToastUtil.showShort(edtName.getHint());
                return;
            }
            if (TextUtils.isEmpty(edtBankBranch.getText())) {
                ToastUtil.showShort(edtBankBranch.getHint());
                return;
            }
            if (TextUtils.isEmpty(edtBankNum.getText())) {
                ToastUtil.showShort(edtBankNum.getHint());
                return;
            }
            if (TextUtils.isEmpty(edtPs.getText())) {
                ToastUtil.showShort(edtPs.getHint());
                return;
            }

            mPresenter.sell(edtNum.getText().toString(), edtBank.getText().toString(), edtBankBranch.getText().toString(), edtBankNum.getText().toString(), edtName.getText().toString(), edtPs.getText().toString());

        });

    }


    public static OneSellFragment newInstance() {
        OneSellFragment fragment = new OneSellFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    /**
     * 设置交易信息
     *
     * @param data
     */
    public void setInfo(SellInfo data) {
        isEnable = true;
        sellerCountTv.setText(data.getShop_num() + getString(R.string.app_oneSellFragment1));
        sellPriceTv.setText("¥" + data.getUsdt_rmb());
        usefulNumTv.setText(getString(R.string.app_oneSellFragment2) + data.getUsable() + "USDT");
        price = Double.parseDouble(data.getUsdt_rmb());
    }

    /**
     * 计算到账金额
     */
    public void computeTotal() {
        double num = 0;
        if (!TextUtils.isEmpty(edtNum.getText())) {
            num = Double.parseDouble(edtNum.getText().toString());
        }
        double total = num * price;
        arriveNumTv.setText(NumberUtil.keep2(total) + " CNY");
    }

    /**
     * 出售成功
     */
    public void sellSuccess() {
        edtPs.getText().clear();
        edtNum.getText().clear();
        SellRecordsActivity.start(getContext());
    }

    public void onGetpayInfoFail(String msg) {
        this.msg = msg;
        isEnable = false;
    }
}
